package com.xon.lotp.service.Impl;

import com.xon.lotp.mapper.UserMapper;
import com.xon.lotp.pojo.User;
import com.xon.lotp.service.UserService;
import com.xon.lotp.utils.TextFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 用户名校验 True为可以使用
     * create by: Xonline
     * create time: 11:07 上午 2021/5/15
     *
     * @return boolean
     * @Param: String userName
     */
    public boolean checkUserName(String userName) {
        if (userMapper.checkUserName(userName) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 校验用户密码是否正确
     * create by: Xonline
     * create time: 11:08 上午 2021/5/15
     *
     * @return String userId
     * @Param: User
     */
    public String checkUserNameAndPwd(User user) {
        return userMapper.checkUserNameAndPwd(user);
    }

    /**
     * 通过UserID查询所有用户信息
     * create by: Xonline
     * create time: 11:09 上午 2021/5/15
     *
     * @return User
     * @Param: String userId
     */
    public User selectUserByUserId(String userId) {
        return userMapper.selectUserByUserId(userId);
    }

    /**
     * 用户注册信息入库
     * create by: Xonline
     * create time: 11:10 上午 2021/5/15
     *
     * @return User
     * @Param: User
     */
    public User addUser(User user) {
        User result = new User();

        // 判断用户名是否可用
        // 生成UUID并格式化
        String uuid = UUID.randomUUID().toString();
        //格式化UUID
        user.setUserId(TextFormat.uuidFormat(uuid));
        //将User写入用户信息表
        int resultInfo = userMapper.addUserForUserInfo(user);
        //判断写入是否成功
        if (resultInfo > 0) {
            //将User写入用户状态表
            int resultStatus = userMapper.addUserForUserStatus(user);
            //判断写入是否成功
            if (resultStatus > 0) {
                result = userMapper.selectUserByUserId(user.getUserId());
            }
        }
        return result;
    }
}
